package tn.softcom.dfbuilder;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

/**
 * Servlet implementation class Generate
 */

public class Generate extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

//		Calendar c = Calendar.getInstance();
//		c.set(2015, 0, 31);
//		// System.out.println(c.getTime());
//
//		if (new Date(System.currentTimeMillis()).after(c.getTime())) {
//			return;
//		}

		String url = request.getParameter("url");
		String operation = request.getParameter("operation");
		File fl = new File(url);
		File template = null;
		Workbook wtemp = null;
		File rapport = null;
		Workbook wrapport = null;
		try {
			File templateFolder = new File(request.getRealPath("")
					+ "/conf/template/");
			File rapportFolder = new File(request.getRealPath("")
					+ "/conf/rapport/");
			if (templateFolder != null && templateFolder.listFiles() != null) {
				template = templateFolder.listFiles()[0];
				wtemp = Utils.getWorkbook(template);
			}
			if (rapportFolder != null && rapportFolder.listFiles() != null) {
				rapport = rapportFolder.listFiles()[0];
				wrapport = Utils.getWorkbook(rapport);
			}

			Workbook w = Utils.getWorkbook(fl);
			EXCELComponent xlsGen = new EXCELComponent(w, wtemp, wrapport);
			xlsGen.readSheet(operation);
			xlsGen.readTemplate();

			//

			File f = xlsGen.writeSheet(request);
			if (f.exists()) {
				int filesize = (int) f.length();
				byte[] buff = new byte[filesize];

				response.setContentType("application/x-msdownload");
				response.setHeader("Content-Disposition",
						"attachment; filename=" + f.getName());
				FileInputStream fin = new FileInputStream(f);
				BufferedInputStream bis = new BufferedInputStream(fin);
				ServletOutputStream fout = response.getOutputStream();
				BufferedOutputStream bos = new BufferedOutputStream(fout);
				int bytesRead;
				while ((bytesRead = bis.read(buff)) != -1) {
					bos.write(buff, 0, bytesRead);
				}
				bos.flush();

				fin.close();
				fout.close();
				bis.close();
				bos.close();
			}

			//
		} catch (IOException ex) {
			System.out
					.println("Be Careful - There is a problem when loadling the workbook : "
							+ ex);
		}
	}

}
